package com.runnor.common.websys.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.runnor.common.websys.system.entity.ApiResult;
import com.runnor.common.websys.system.entity.Sysdict;
import com.runnor.common.websys.system.entity.bo.SysDictBo;
import com.runnor.common.websys.system.service.ISysdictService;
import com.runnor.common.websys.system.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author runnor-yh
 * @date 2023年08月15日 9:37
 */
@RestController
@RequestMapping("api/sysdict")

public class SysdictController {
    @Autowired
    ISysdictService sysdictService;
    @Autowired
    SysMenuService menuService;

    /**
     * 分页查询
     */
    @PreAuthorize("hasAuthority('sysdict:list')")
    @GetMapping("/page")
    public ResponseEntity page(@RequestParam(defaultValue = "1") Integer current,
                               @RequestParam(defaultValue = "10") Integer size,
                               Sysdict sysdict) {

        QueryWrapper<SysDictBo> queryWrapper = new QueryWrapper<>();

        queryWrapper.like(StringUtils.hasText(sysdict.getCode()), "code", sysdict.getCode())
                .like(StringUtils.hasText(sysdict.getName()), "name", sysdict.getName())
                .like(StringUtils.hasText(sysdict.getValue()), "value", sysdict.getValue())
                .and(StringUtils.hasText(sysdict.getParentCode()), qw -> {
                    qw.eq("parent_code", sysdict.getParentCode())
                            .or().eq("code", sysdict.getParentCode());
                })
                .eq(sysdict.getEnable() != null, "enable", sysdict.getEnable())
                .eq("deleted", 0);

        queryWrapper.orderByAsc("sort").orderByDesc("create_time");

        Page data = sysdictService.pages(new Page(current, size), queryWrapper);
        return ResponseEntity.ok(ApiResult.ok(data));
    }

    /**
     * 根据code查询字典
     */
    @GetMapping("/getById")
    public ResponseEntity getById(String code) {
        Sysdict sysdict = sysdictService.getById(code);
        return ResponseEntity.ok(ApiResult.ok(sysdict));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('sysdict:add')")
    public ResponseEntity save(Sysdict sysdict) {

        //判断 所属父类 等于空  赋值 null
        if (!StringUtils.hasText(sysdict.getParentCode())) {
            sysdict.setParentCode(null);
        }

        boolean save = sysdictService.save(sysdict);

        return ResponseEntity.ok(ApiResult.ok(save));
    }

    /**
     * 修改
     */
    @PostMapping("/updateById")
    @PreAuthorize("hasAuthority('sysdict:edit')")
    public ResponseEntity updateById(Sysdict sysdict) {

        //判断 所属父类 等于空  赋值 null
        if (!StringUtils.hasText(sysdict.getParentCode())) {
            sysdict.setParentCode(null);
        }

        boolean save = sysdictService.updateById(sysdict);
        return ResponseEntity.ok(ApiResult.ok(save));
    }

    /**
     * 删除
     */
    @GetMapping("/deletes")
    @PreAuthorize("hasAuthority('sysdict:del')")
    public ResponseEntity deletes(String codes) {
        boolean b = sysdictService.removeBatchByIds(Arrays.asList(codes.split(",")));
        return ResponseEntity.ok(ApiResult.ok(b));
    }

    /**
     * 删除
     */
    @GetMapping("/removeById")
    @PreAuthorize("hasAuthority('sysdict:del')")
    public ResponseEntity removeById(String code) {
        boolean b = sysdictService.removeById(code);
        return ResponseEntity.ok(ApiResult.ok(b));
    }

    /**
     * 根据code修改 状态
     */
    @GetMapping("/updateEnableByCode")
    @PreAuthorize("hasAuthority('sysdict:edit')")
    public ResponseEntity updateEnableByCode(String code, Boolean enable) {

        UpdateWrapper updateWrapper = new UpdateWrapper();

        updateWrapper.set("enable", enable);
        updateWrapper.eq("code", code);

        boolean update = sysdictService.update(null, updateWrapper);

        return ResponseEntity.ok(ApiResult.ok(update));
    }

    /**
     * 功能管理树
     */
    @GetMapping("/getSysDictListTree")
    public ResponseEntity getSysMenuListTree() {
        List<Sysdict> list = sysdictService.getSysDictListTree();
        return ResponseEntity.ok(ApiResult.ok(list));
    }

    /**
     * 根据 parentCode 查询 list
     */
    @GetMapping("/getListByParentCode")
    public ResponseEntity getSysMenuListTree(String parentCode) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(StringUtils.hasText(parentCode), "parent_code", parentCode);
        queryWrapper.eq("enable", 1);
        queryWrapper.eq("deleted", 0);
        queryWrapper.orderByAsc("sort");
        List<Sysdict> list = sysdictService.list(queryWrapper);
        return ResponseEntity.ok(ApiResult.ok(list));
    }

    @GetMapping("/getSysConfig")
    public ResponseEntity getSysConfig() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("code,value");
        wrapper.eq("parent_code", "WebSite_Config");
        wrapper.eq("enable", 1);
        wrapper.eq("deleted", 0);
        List<Sysdict> list = sysdictService.list(wrapper);

        LinkedHashMap<String, Object> resultMap = new LinkedHashMap<>();
        list.forEach(e -> resultMap.put(e.getCode(), e.getValue()));

        resultMap.put("routes", menuService.getSysMenuRoutesTree());

        return ResponseEntity.ok(ApiResult.ok(resultMap));

    }


}
